﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static WinFormsApp1.Leo.week02.SlaughteredAdd;
using static WinFormsApp1.TongShiWei.AddScaleContext;
using static WinFormsApp1.WangHaoQi.Week02Winfroms.SlaughterWinFormAdd;

namespace WinFormsApp1.WangHaoQi.Week02Winfroms
{
    public partial class SlaughterWinFormAdd : Form
    {
        public SlaughterWinFormAdd()
        {
            InitializeComponent();

            //屠宰类型下拉框
            List<SlaughterTypes> data = new List<SlaughterTypes>()
            {
                new SlaughterTypes{Id=1,Name="烫毛"},
                new SlaughterTypes{Id=1,Name="扒皮"}
            };

            uiComboBox1.DataSource = data;
            uiComboBox1.DisplayMember = "Name";
            uiComboBox1.ValueMember = "Id";

            this.Controls.Add(uiComboBox1);
            GetLive();
            GetUPCData();
        }


        //商品代号
        public async void GetUPCData()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<UPCDataser>>>(URLCommon.Url2, "/WeekListOne_Leo/GetUPCDataShow", HttpClientHelper.MethodType.GET, default);

            uiComboBox3.DataSource = items.Data;
            uiComboBox3.DisplayMember = "UPCDataName";
            uiComboBox3.ValueMember = "GoodsMinuteId";

            this.Controls.Add(uiComboBox3);
        }
        public class UPCDataser
        {
            public int GoodsMinuteId { get; set; }
            public string UPCDataName { get; set; }
        }
        //活畜单号
        public async void GetLive()
        {

            var items = await HttpClientHelper.RequestUrl<LiveAnimakses, ApiResult<List<LiveAnimakses>>>(URLCommon.Url2, "/LiveAnimals/LiveAnimalGet", HttpClientHelper.MethodType.GET);

            uiComboBox2.DataSource = items.Data;
            uiComboBox2.DisplayMember = "LiveAnimalsDocument";
            uiComboBox2.ValueMember = "LiveAnimalsId";

        }
        public class LiveAnimakses
        {
            public int LiveAnimalsId { get; set; }
            /// <summary>
            /// 单据号
            /// </summary>
            public string LiveAnimalsDocument { get; set; }
        }

        /// <summary>
        /// 供应商下拉框绑定
        /// </summary>
        //public async void SupplierShow()
        //{
        //    var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<Suppliers>>>("http://localhost:5106/api/CheckIn/", "SupplierShow", HttpClientHelper.MethodType.POST, null);
        //    uiComboBox2.DataSource = items.Data;
        //    uiComboBox2.DisplayMember = "SupplierName";
        //    uiComboBox2.ValueMember = "SupplierId";

        //    this.Controls.Add(uiComboBox2);
        //}
        /// <summary>
        /// 进场登记
        /// </summary>
        public class CheckIns
        {
            /// <summary>
            /// 进场登记id
            /// </summary>
            public int CheckInId { get; set; }
            /// <summary>
            /// 进场登记编号
            /// </summary>
            public string CheckInNo { get; set; }
        }

        private async void uiButton2_Click(object sender, EventArgs e)
        {
            var slaughterDate = Convert.ToDateTime(uiDatetimePicker1.Text);
            var slaughterDocument = "";

            var slaughterMaxType = Convert.ToInt32(uiComboBox1.SelectedValue);
            var slaughterNo = uiTextBox3.Text;
            var slaughterNum = Convert.ToInt32(uiTextBox1.Text);
            var checkInId = Convert.ToInt32(uiComboBox3.SelectedValue);
            var projectDateTime = uiTextBox4.Text;
            var resttime = uiTextBox8.Text;
            var slaughterRemark = uiTextBox10.Text;
            var SlaughterSDate = DateTime.Now;

            Slaughter slaughter = new Slaughter();

            slaughter.SlaughterDate = slaughterDate;
            slaughter.SlaughterDocument = slaughterDocument;
            slaughter.CheckInId = checkInId;
            if (uiRadioButton1.Checked)
            {
                slaughter.SlaughterType = 1;
            }
            if (uiRadioButton2.Checked)
            {
                slaughter.SlaughterType = 2;
            }
            slaughter.SlaughterMaxType = slaughterMaxType;
            slaughter.SlaughterNo = slaughterNo;
            slaughter.DepotId = 1;
            slaughter.SlaughterSDate = SlaughterSDate;
            slaughter.SlaughterNum = slaughterNum;
            slaughter.ProjectDateTime = projectDateTime;
            slaughter.SlaughteredId = 1;
            slaughter.Resttime = resttime;
            slaughter.SlaughterRemark = slaughterRemark;
            slaughter.LiveAnimalsId = Convert.ToInt32(uiComboBox2.SelectedValue);
            slaughter.IsDelete = false
            ;

            var res = await HttpClientHelper.RequestUrl<Slaughter, ApiResult<Slaughter>>(URLCommon.Url1, "/Slaughters/AddSlaughter", HttpClientHelper.MethodType.POST, slaughter);
            if (res.Code == 1)
            {
                //弹框提示
                MessageBox.Show("添加成功");
                //创建新页面
                //SlaughterWinFormShow slaughterShow = new SlaughterWinFormShow();
                //显示 创建的新页面
                //slaughterShow.Show();
                //关闭本页面
                this.Hide();
            }
        }



        /// <summary>
        /// 批宰计划字段
        /// </summary>
        public class Slaughter
        {
            /// <summary>
            /// 计划屠宰日期 
            /// </summary>
            public DateTime? SlaughterDate { get; set; }

            /// <summary>
            /// 单据号
            /// </summary>
            public string SlaughterDocument { get; set; }
            /// <summary>
            /// 进场登记Id
            /// </summary>
            public int CheckInId { get; set; }
            /// <summary>
            /// 送宰类型
            /// </summary>
            public int SlaughterType { get; set; }
            /// <summary>
            /// 屠宰类型
            /// </summary>
            public int SlaughterMaxType { get; set; }
            /// <summary>
            /// 屠宰编号
            /// </summary>
            public string SlaughterNo { get; set; }
            /// <summary>
            /// 仓库Id
            /// </summary>
            public int DepotId { get; set; }
            /// <summary>
            /// 生产日期
            /// </summary>
            public DateTime? SlaughterSDate { get; set; }
            /// <summary>
            /// 计划屠宰头数
            /// </summary>
            public int SlaughterNum { get; set; }
            /// <summary>
            /// 计划时间 大概需要多长时间
            /// </summary>
            public string ProjectDateTime { get; set; }
            /// <summary>
            /// 待宰圈管理Id
            /// </summary>
            public int SlaughteredId { get; set; }
            /// <summary>
            /// 静养时间
            /// </summary>
            public string Resttime { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string SlaughterRemark { get; set; }
            /// <summary>
            /// 活畜称重单Id
            /// </summary>
            public int LiveAnimalsId { get; set; }
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; }
        }

        /// <summary>
        /// 
        /// </summary>
        public class SlaughterTypes
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            //关闭本页面
            this.Hide();
        }

        private void SlaughterWinFormAdd_Load(object sender, EventArgs e)
        {

        }
    }
}
